package org.balthie.demo.sum.misc.direct;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarFile;

import org.openjdk.jol.info.ClassLayout;

public class DirectMemoryJarFileTest
{
    public static void loadJar() throws IOException
    {
//        SecurityManager
//        ClassLoader
//        ProtectionDomain
//        WeakReference
//        Inflater
        
        System.out.println(" begin load jar ");
        DirectByteBufferPrinter.printBits();
        
        for(int i=0;i<1000;i++)
        {
            try
            {
                JarFile jar = new JarFile("E:\\balthie-git\\viomi-gitlab\\viomi-jira\\viomi-jira-executor\\target\\viomi-jira-executor.jar");
                
                DirectByteBufferPrinter.printBits();
                
                jar.stream().forEach(entry -> {
                    try
                    {
                        jar.getEntry(entry.getName());
                        TimeUnit.MILLISECONDS.sleep(1);
                    }
                    catch (InterruptedException e)
                    {
                    }
                });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
       
        DirectByteBufferPrinter.printBits();
        
    }
    
    public static Class<?> loadClasses() throws MalformedURLException, URISyntaxException, ClassNotFoundException
    {
        File f = new File("E:\\Program Files\\Java\\jdk1.8.0_65\\jre\\lib\\rt.jar");
//        URL url = new URL("E:\\balthie-git\\viomi-gitlab\\viomi-jira\\viomi-jira-executor\\target\\viomi-jira-executor.jar");
        JarFileClassLoader classLoader = new JarFileClassLoader(new URL[] { f.toURI().toURL() });
        
        System.out.println(ClassLayout.parseInstance(classLoader).toPrintable());
        System.out.println(ClassLayout.parseInstance(new Object()).toPrintable());
        
        System.out.println(ClassLayout.parseInstance(DirectMemoryJarFileTest.class).toPrintable());
        System.out.println(ClassLayout.parseInstance(Object.class).toPrintable());
        
        Class<?> findClass = classLoader.loadClass("java.lang.Object");
        
        
        return findClass;
    }
    
    public static void main(String[] args) throws IOException, URISyntaxException, ClassNotFoundException
    {
//        loadJar();
        
        loadClasses();
        
        try
        {
            TimeUnit.HOURS.sleep(100);
        }
        catch (InterruptedException e)
        {
        }
    }
}
